package com.black.app.service.impl.store;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.black.app.domain.req.my.myshoppingcar.AppShoppingCarAddReq;
import com.black.app.domain.req.my.myshoppingcar.AppShoppingCarEditReq;
import com.black.app.domain.req.my.myshoppingcar.AppShoppingCarQueryReq;
import com.black.app.domain.resp.my.myshoppingcar.AppShoppingCarResp;
import com.black.app.domain.resp.my.myshoppingcar.UserShoppingCarResp;
import com.black.app.domain.store.AppStoreShoppingCart;
import com.black.app.service.store.AppStoreShoppingCartService;
import com.black.app.mapper.store.AppStoreShoppingCartMapper;
import com.black.common.exception.database.PrimaryException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Objects;

/**
* @author admin
* @description 针对表【app_store_shopping_cart】的数据库操作Service实现
* @createDate 2024-10-31 11:15:37
*/
@Service
public class AppStoreShoppingCartServiceImpl extends ServiceImpl<AppStoreShoppingCartMapper, AppStoreShoppingCart>
    implements AppStoreShoppingCartService{

    /**
     * 后台—购物车分页查询
     * @param req
     * @return
     */
    @Override
    public PageInfo<AppShoppingCarResp> paginationQuery(AppShoppingCarQueryReq req) {
        return PageHelper.startPage(req.getPageNum(), req.getPageSize()).doSelectPageInfo(() -> {
            this.baseMapper.paginationQuery(req);
        });
    }

    /**
     * App—我的购物车列表
     * @param userId
     * @return
     */
    @Override
    public PageInfo<UserShoppingCarResp> getMyShoppingCarList(Integer userId) {
        return PageHelper.startPage(1, 10).doSelectPageInfo(() -> {
            this.baseMapper.getMyShoppingCarList(userId);
        });
    }

    /**
     * App—购物车新增
     * @param req
     * @return
     */
    @Override
    public boolean add(AppShoppingCarAddReq req) {
        if(req.getQuantity()==0)
            req.setQuantity(1);
        // 判断要添加的商品在购物车中（userId、 ProductId和SpecificationId都相同）是否存在
        // 如果存在，则修改数量
        AppStoreShoppingCart cart = this.baseMapper.getShoppingCarByProductIdAndSpecificationId(req.getUserId(), req.getProductId(), req.getSpecificationId());
        if(Objects.nonNull(cart)){
            //修改请求实例
            AppShoppingCarEditReq editReq = new AppShoppingCarEditReq();
            //根据userId、 ProductId和SpecificationId获取购物车对象
            editReq.setCartId(cart.getCartId());
            editReq.setSpecificationId(req.getSpecificationId());
            editReq.setQuantity(cart.getQuantity()+req.getQuantity());
            //执行修改操作
            return this.edit(editReq);
        }

        AppStoreShoppingCart newEntity = new AppStoreShoppingCart();

        BeanUtils.copyProperties(req, newEntity);
        newEntity.setCreateTime(new Date());
        return this.save(newEntity);
    }

    /**
     * App—购物车修改
     * @param req
     * @return
     */
    @Override
    public boolean edit(AppShoppingCarEditReq req) {
        //根据购物车id获取购物车对象
        AppStoreShoppingCart cart = this.getById(req.getCartId());
        cart.setSpecificationId(req.getSpecificationId());
        cart.setQuantity(req.getQuantity());
        return this.updateById(cart);
    }

    /**
     * App—购物车删除
     * @param ids
     * @return
     */
    @Override
    public boolean deleteById(Integer[] ids) {
        if(Objects.isNull(ids) || ids.length == 0){
            throw new PrimaryException();
        }

        for (Integer id : ids){
            AppStoreShoppingCart cart = this.getById(id);
            if(Objects.nonNull(cart)){
                this.removeById(id);
            }
        }
        return true;
    }
}




